using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 调用者，负责调用命令
/// </summary>
public class Invoker{
    private FIFOList<AbstractCommand> commands;

    public Invoker(){
        commands = new FIFOList<AbstractCommand>();
    }

    public void AddCommand(AbstractCommand command){
        commands.Enqueue(command);
    }

    public void ExecuteCommand(){
        while(commands.Count > 0){
            commands.Dequeue()?.Execute();
        }
        // foreach(var command in commands){
        //     command?.Execute();
        // }
    }

    public void RemoveCommand(AbstractCommand command){
        commands.Remove(command);
    }

    public void RevocationCommand(){
        commands.Dequeue();
    }
}


/// <summary>
/// 先进先出列表
/// </summary>
/// <typeparam name="T"></typeparam>
public class FIFOList<T> : IEnumerable<T> where T : notnull{
    private List<object> keys;
    private LinkedList<T> lists;
    public int Count => lists.Count;

    public FIFOList(){
        keys = new List<object>();
        lists = new LinkedList<T>();
    }

    public T Enqueue(T item){

        lists.AddLast(item);
        keys.Add(item.GetHashCode());
        return item;
    }

    public T? Dequeue(){
        if(lists.Count == 0){
            return default;
        }
        if(lists.First is not null){
            T item = lists.First.Value;
            lists.RemoveFirst();
            return item;
        }
        return default;
    }

    public void Remove(T item){
        lists.Remove(item);
        keys.Remove(item.GetHashCode());
    }

    public IEnumerator<T> GetEnumerator(){
        return lists.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return lists.GetEnumerator();
    }
}